#include<iostream>

using namespace std;

typedef long long ll;

ll ksm(ll base,ll power) 
{
    ll result=1;
    while(power>0) 
    {
        if(power&1) 
            result=result*base;
        power>>=1;
        base=base*base;
    }
    return result;
}

const int F=1111;

int a[F];

int main()
{
	ll ans=0;
	int n,jz,type=0;
	cin>>n>>jz;
	while(jz)
		a[++type]=jz%2,jz/=2;
	for(int i=type;i>=1;i--)
		ans+=ksm(n,i-1)*a[i];
	cout<<ans;
	return 0;
}